#include <iostream>
#include <string>
#include <algorithm>
#include <memory>
using namespace std;
namespace my_memory
{
    template <typename T>
    class MemPool
    {
    public:
        MemPool()
            : _mem(nullptr), _sumSize(0), _list(nullptr)
        {
            _objSize = max(sizeof(T), sizeof(void *));
        }
        T *New()
        {
            if (_list != nullptr)
            {
                T *ret = (T *)_list;
                _list = *(void **)_list;
                new (ret) T;
                return ret;
            }
            if (_sumSize < _objSize)
            {
                _mem = (char *)malloc(128 * 1024);
                // if (_mem == nullptr)
				// {
				// 	throw std::bad_alloc();
				// }
                _sumSize = 128 * 1024;
            }
            T *ret = (T *)_mem;
            _mem += _objSize;
            _sumSize -= _objSize;
            new (ret) T;
            return ret;
        }
        void Delete(T *p)
        {
            p->~T();
            *(void **)p = _list;
            _list = p;
        }
        ~MemPool()
        {
        }

    private:
        char *_mem;
        size_t _sumSize;
        void *_list;
        size_t _objSize;
    };
}